<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="format-detection" content="telephone=no">
<title></title>
<link rel="stylesheet" type="text/css" href="{$Think.config.RESOURCEURL}wx_assets/css/product.css?v=201408281">
	<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/alertMsg.js"></script>
<script type='text/javascript' src='{$Think.config.RESOURCEURL}wx_assets/js/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src="{$Think.config.RESOURCEURL}wx_assets/js/base.js"></script>
	<style>
		.load-more {
			margin-left: auto;
			margin-right: auto;
			font-size: 15px;
			color: #3985FF !important;
			height: 23px;
			line-height: 23px;
			/*border: 1px solid #3e3e3e;*/
			display: block;
			width: 100%;
			text-align: center;
			background: #fff;
			padding:10px 0;
			/*background-color: #5d5554;*/
			/*background-image: -ms-linear-gradient(top, #5c5453 0%, #4b4443 100%);*/
			/*background-image: linear-gradient(top, #5c5453 0%, #4b4443 100%);*/
			/*background-image: -webkit-gradient(linear,0 0%,0% 100%,from(#5c5453),to(#4b4443));*/
		}
		.load-more:before{
			content: ' ';width: 8px;height: 8px;border-top: 1px solid #7d7d7d;
			border-left: 1px solid #7d7d7d;position:relative;display:inline-block;
			border-color:#3985FF;-webkit-transform: rotate(225deg);margin-right: 10px;margin-bottom: 3px;
		}
	</style>
</head>
<body id="page_allMenu">
	<div class="center">
		<nav id="navBar" style="height: 356px;">
		<dl>
			<!--dt>分类</dt-->
			<volist name="tags" id="tag" >
			<eq name="key" value="0">
			<dd tagid={$tag.id} tagnum="{$tag.count}" class="active">{$tag.name}
				<if condition="$tag.reserveCount gt 0">
				<span style="display: block;">{$tag.reserveCount}</span> 
				<else /><span style="display: none;"></span> 
				</if>
			</dd>
			</eq>
			<neq name="key" value="0">
			<dd tagid={$tag.id} tagnum="{$tag.count}">{$tag.name}
				<if condition="$tag.reserveCount gt 0">
				<span style="display: block;">{$tag.reserveCount}</span> 
				<else /><span style="display: none;"></span> 
				</if>
			</dd>
			</neq>
			</volist>
		</dl>
		</nav>

		<section id="infoSection" style="height: 356px;">
		<article style="overflow:scroll;overflow-y:hidden;overflow-x:hidden "> <!--div class="h2">推荐菜</div-->
		<div id="pInfo" data-count="{$tag.count}">
			<volist name="products.productList" id="product" >
				<php>if($i <= 9){</php>
					<dl id='{$product.id}' class="unchoose" dunitname="{$product.unitName}" dsubcount="2203"
						dishid='{$product.id}' dname="{$product.name}" dtaste="" ddescribe="" data-inventory="{$product.inventory}" data-inventoryCount="{$product.inventoryCount}"
						dprice="{$product.fixedPrice}" dishot="2" ddiscountedprice="{$product.discountedPrice}" disspecial="1" shopinfo="">
						<dt onclick="onDetail({$product.id})">
						<h3>{$product.name}</h3>
						</dt>
						<dd>
							<a href="javascript:void(0)" class="dataIn">
								<img src='{$product.picUrl}' alt="" title="" onclick="onDetail({$product.id})">
								<!-- <span></span> -->
							</a>
						</dd>
						<empty name="product.picUrl">
							<script>
								$('#{$product.id}').css('paddingLeft', '5px');
							</script>
						</empty>
						<dd>
							<notempty name="product.fixedPrice">
								<empty name="product.discountedPrice">
									<empty name="product.unitName">
										<em> {$product.fixedPrice}元 </em>
									</empty>
									<notempty name="product.unitName">
										<em> {$product.fixedPrice}元/{$product.unitName} </em>
									</notempty>
								</empty>
								<notempty name="product.discountedPrice">
									<empty name="product.unitName">
										<em> {$product.discountedPrice}元
											<del>{$product.fixedPrice}元 </del>
										</em>
									</empty>
									<notempty name="product.unitName">
										<em> {$product.discountedPrice}元/{$product.unitName}
											<del> {$product.fixedPrice}元/{$product.unitName} </del>
										</em>
									</notempty>
								</notempty>
							</notempty>
						</dd>

						<dd class="dpNum">{$product.salesCount}人点过</dd>
						<if condition="($showBtn eq 1)">
							<dd class="btn">
								<button class="minus" style="display: inline-block; "><strong></strong></button>
								<i style="display: inline-block; ">{$product.count}</i>
								<button class="add"><strong></strong></button>
							</dd>
						</if>
					</dl>
				<php>}</php>
			</volist>

		</div>
			<php> if( 9<$products['total']){</php>
			<div class="jx_map" style="width:100%;padding:10px 0">
				<a class="load-more" href="javascript:void(0);" onclick="loadmore(this);" data-start = "9" data-total="{$products['total']}" style="color:#000000">点击加载更多</a>
			</div>
			<php>}</php>
		</article>
		</section>
		
		<if condition="($showBtn eq 1)">
		<footer class="footFix">
		<button class="btn_change" id="btn_menu" onclick="return doMenu()">
			购物车
			<!--<span class="num">4</span> -->
		</button>
		</footer>
		</if>
	</div>
	
	<script>
	
	var hrefParamsArray = GetSessionIdFromHref(window.location.search);
	hrefParamsArray["userName"]='{$userName}';
	function GetSessionIdFromHref(search){
	　　	var args = search.substring(1); 
	　　	var retval = new Array(); 
		args = args.split("&"); 
			for(var i = 0; i < args.length; i ++){ 
				str = args[i]; 
				var arg = str.split("="); 
				if(arg.length <= 1) continue; 
				
				if(arg[0] == 'params') {
					var params = decodeURIComponent(arg[1]);
					var obj = eval('(' + params + ')');
					retval["mid"] = obj.id;
				} else if(arg[0] == 'customerId') {
					retval["customerId"] = arg[1]; 
				}else if(arg[0] == 'userName'){
					retval["userName"] = arg[1]; 
				}
			} 
			
			if (retval["userName"] == '') {
				retval["userName"] = '{$userName}';
			}
			return retval; 
	}
	
    function setHeight(){
        var cHeight;
        cHeight = document.documentElement.clientHeight;
        cHeight = cHeight +"px"
        document.getElementById("navBar").style.height =  cHeight;
        document.getElementById("infoSection").style.height =  cHeight;
    }
	function onDetail(id) {
		var rootid = '';
		if (hrefParamsArray["rootid"] !== undefined) {
			rootid = ',"rootid":' + hrefParamsArray["rootid"];
		}
		window.location.href = 'call.html?userName=' + hrefParamsArray['userName'] + '&customerId='
		+ hrefParamsArray['customerId'] + '&module=product&method=getProduct&params={"tplid":4,"id":"' + id + rootid + '"}';
	}
    //ajax处理
    function doSelect(){
		var dds = _qAll('#navBar dd');
        var aa=0, bb;
        var article = _q("#infoSection article");
        for (aa;aa<dds.length;aa++){
            dds[aa].onclick = function(){
				var me = $(this);
                _q('.active').className = null;
                this.className = "active";
                var div =  document.getElementById("pInfo");
                var params;
                if ('{$groupBy}' == 'category') {
                    params = {
                        'module': 'product',
                        'method' : 'getProductListByCategory',
                        'params' : '{"cid":' +  this.getAttribute('tagid') + ',"counts":' +  9 + ',"tplid":"4"}'
                    };
                } else {
					var p =  '{"sid":-1,"tagId":' +  this.getAttribute('tagid') +
								',"counts":' + 9 + ',"cids":"","type":"buy","tplid":"4"}';
                    params = {
                        'module': 'product',
                        'method' : 'getProductByTagId',
                        'params' : p
                    };

                }

    			var urlString = 'call.json?customerId='+ hrefParamsArray["customerId"]+'&userName='+hrefParamsArray["userName"];
    			$.ajax({url : urlString,
    					dataType : "json",
    					type : "post",
    					data : params,
    					success : function(data) {
   							if (data.code != undefined) {
   								alert(data.msg);
   							}
       	                    var dishList = data['list'][0].productList;
       	                    var tagId = data['tagId'];
       	                    var str = '';
       	                    for(key in dishList) {
       	                        var dish = dishList[key];
       	                        if (dish['fixedPrice'] != 0 && dish['fixedPrice'] != null) {
       	                        	if (dish['discountedPrice'] == 0 || dish['discountedPrice'] == null) {
           	                        	if (!dish.unitName || dish.unitName == '') {
           	                        		var priceHtml = "<em>"+dish['fixedPrice']+"元</em>";
           	                        	} else {
           	                        		var priceHtml = "<em>"+dish['fixedPrice']+"元/" + dish["unitName"] + "</em>";
           	                        	}
           	                        } else  {
           	                        	if (!dish.unitName || dish.unitName == '') {
           	                        		 var priceHtml = "<em>"+dish['discountedPrice']+"元<del>"+dish['fixedPrice']+"元</del></em>";
           	                        	} else {
           	                        		 var priceHtml = "<em>"+dish['discountedPrice']+"元/" + dish["unitName"] + "<del>"+dish['fixedPrice']+"元/" + dish["unitName"] + "</del></em>";
           	                        	}
           	                        }
       	                        } else {
       	                        	var priceHtml = '';
       	                        }

       	                        var hotHtml = '';
       	                        var dSubCountHtml = '';
   								if (dish['salesCount']) {
   		                            var dSubCountHtml = dish['salesCount'] + '人点过';
   		                        } else {
   		                            var dSubCountHtml = '';
   		                        }
       	                        var attrList = " id='" + dish['id'] + "' dUnitName='"+dish['unitName']+"' dSubCount='"+dish['dSubCount']+"' dishid='"+dish['id']
       	                       	+"' dname='"+dish['name']+"' dTaste='"+dish['dTaste']+"' dPrice='"+dish['fixedPrice']
       	                       	+"' dIsHot='"+dish['dIsHot']+"' ddiscountedprice='"+dish['discountedPrice']+"' dIsSpecial='"+dish['dIsSpecial']+"' data-inventory='"+dish['inventory']+"' data-inventoryCount='"+dish['inventoryCount']+"' ";

   								if (dish['picUrl'] == '') {
   									attrList += " style='padding-left:5px;' ";
   								}

   								str += "<dl shopInfo=''"+attrList+"' data-inventory='"+dish['inventory']+"' data-inventoryCount='"+dish['inventoryCount']+"'><dt><h3 onclick=\"onDetail(\'"+dish.id+"\')\">"+dish['name']
   								+"</h3></dt><dd><a href='javascript:void(0)' class='dataIn'><img src='" + dish['picUrl'] + "' alt='' title='' onclick=\"onDetail(\'"+dish.id+"\')\" />"
   								+hotHtml+"</a></dd><dd>"+priceHtml+"</dd><dd class='dpNum'>"+dSubCountHtml
   								+"</dd>";
   								if ({$showBtn} == 1){
   									str += "<dd class='btn'><button class='minus'><strong></strong></button><i style='display: inline-block;'>"+dish['count']+"</i><button class='add'><strong></strong></button></dd>";
   								}
   								str += "</dl>";
       	                    }
								if(data['list'][0].total>9){

									var loadstr='<div class="jx_map" style="width:100%;padding:10px 0">\
									<a class="load-more" href="javascript:void(0);" onclick="loadmore(this)" data-start = "'+dishList.length+'" data-total="'+data['list'][0].total+'" style="color:#000000">点击加载更多</a>\
									</div>';
									if($(".load-more").length!=0){
										$(".load-more").parent().remove();
									}
									$("#pInfo").parent().append(loadstr);
								}else{
									if($(".load-more").length!=0){
										$(".load-more").parent().remove();
									}
								}
								if (_q('.active').getAttribute('tagid') == tagId) {
									div.innerHTML = str;
									_q('#infoSection').scrollTop = 0;

									var type = '{$type}';
									if ({$showBtn} == 1) {
										doSelectBtn();
									}
								}

    					},
    					error : function() {
    					}
    				});
            }
        }
    }
	function loadmore(obj){
		var dd = $("#navBar dd.active");
		var me = $(obj);
		var LIMIT = 9;
		var total = me.data("total");
		var start = me.data("start");
		var p =  '{"sid":-1,"tagId":' +  dd.attr('tagid') +
								',"counts":'+(total-start)+ ',"cids":"","type":"buy","tplid":"4","start":'+start+',"limit":'+(total-start)+'}';
		var params = {
			'module': 'product',
			'method' : 'getProductByTagId',
			'params' : p
		};
		var urlString = 'call.json?customerId='+ hrefParamsArray["customerId"]+'&userName='+hrefParamsArray["userName"];
		$.ajax({url : urlString,
			dataType : "json",
			type : "post",
			data : params,
			success : function(data) {
				if (data.code != undefined) {
					alert(data.msg);
				}
				var dishList = data['list'][0].productList;
				if(dishList.length<=9){$(".jx_map").hide();}
				var tagId = data['tagId'];
				var str = '';
				for(key in dishList) {

					var dish = dishList[key];
					if (key < LIMIT) {
						if (dish['fixedPrice'] != 0 && dish['fixedPrice'] != null) {
							if (dish['discountedPrice'] == 0 || dish['discountedPrice'] == null) {
								if (!dish.unitName || dish.unitName == '') {
									var priceHtml = "<em>" + dish['fixedPrice'] + "元</em>";
								} else {
									var priceHtml = "<em>" + dish['fixedPrice'] + "元/" + dish["unitName"] + "</em>";
								}
							} else {
								if (!dish.unitName || dish.unitName == '') {
									var priceHtml = "<em>" + dish['discountedPrice'] + "元<del>" + dish['fixedPrice'] + "元</del></em>";
								} else {
									var priceHtml = "<em>" + dish['discountedPrice'] + "元/" + dish["unitName"] + "<del>" + dish['fixedPrice'] + "元/" + dish["unitName"] + "</del></em>";
								}
							}
						} else {
							var priceHtml = '';
						}

						var hotHtml = '';
						var dSubCountHtml = '';
						if (dish['salesCount']) {
							var dSubCountHtml = dish['salesCount'] + '人点过';
						} else {
							var dSubCountHtml = '';
						}
						var attrList = " id='" + dish['id'] + "' dUnitName='" + dish['unitName'] + "' dSubCount='" + dish['dSubCount'] + "' dishid='" + dish['id']
								+ "' dname='" + dish['name'] + "' dTaste='" + dish['dTaste'] + "' dPrice='" + dish['fixedPrice']
								+ "' dIsHot='" + dish['dIsHot'] + "' ddiscountedprice='" + dish['discountedPrice'] + "' dIsSpecial='" + dish['dIsSpecial'] + "'data-inventory='"+dish['inventory']+"' data-inventoryCount='"+dish['inventoryCount']+"' ";

						if (dish['picUrl'] == '') {
							attrList += " style='padding-left:5px;' ";
						}

						str += "<dl shopInfo=''" + attrList + "'><dt><h3 onclick=\"onDetail(\'" + dish.id + "\')\">" + dish['name']
						+ "</h3></dt><dd><a href='javascript:void(0)' class='dataIn'><img src='" + dish['picUrl'] + "' alt='' title='' onclick=\"onDetail(\'" + dish.id + "\')\" />"
						+ hotHtml + "</a></dd><dd>" + priceHtml + "</dd><dd class='dpNum'>" + dSubCountHtml
						+ "</dd>";
						if ({$showBtn} == 1) {
							str += "<dd class='btn'><button class='minus'><strong></strong></button><i style='display: inline-block;'>" + dish['count'] + "</i><button class='add'><strong></strong></button></dd>";
						}
						str += "</dl>";
					}
				}
				$("#pInfo").append(str);
				$(".load-more").parent().remove();
				doSelectBtn();

			},
			error : function() {
			}
		});
	}

    _forEach = function(arr, callback) { 
    	if (typeof arr === 'string'){ 
    		try{arr = _qAll(arr);}
    		catch(ex){console.log(ex);return;} 
    	}
    	Array.prototype.forEach.call(arr, callback); 
    };
    function countDish(){
        var total = 0,countdish;
        var dishNum = _qAll("#page_allMenu section article dl .btn i");
        _forEach(dishNum,function(ele,idx,dishNum){
            countdish = parseInt(ele.innerHTML);
            if(countdish>0){
                total++;
            }
        });

        if(total != 0){
        	_q("#page_allMenu nav dl dd.active span").innerHTML = total;
        	_q("#page_allMenu nav dl dd.active span").style.display='block';
        }else{
        	_q("#page_allMenu nav dl dd.active span").style.display='none';
        }
    }

    function btnShowHide(num,btns){
        countDish();
        if (num <= 0) {
            btns.children[0].style.display ="none";
            btns.children[1].style.display ="none";
        }else{
            btns.children[0].style.display ="inline-block";
            btns.children[1].style.display ="inline-block";
        };
    }
	$(".add").live("click",function(){
		var originalNum,countNumText;
		var inventory = $(this).parent().parent().data("inventory");
		var inventoryCount = $(this).parent().parent().data("inventorycount");
		var _self = this;
		var dishid = _self.parentNode.parentNode.getAttribute('dishid');
		originalNum = parseInt(_self.parentNode.children[1].innerHTML, 10);
		countNumText =  originalNum +1;
		if(inventory==1&&countNumText>inventoryCount){
			alert("超出库存数量");
			return;
		}
		var shopInfo =_self.parentNode.parentNode.getAttribute('shopInfo');
		_self.parentNode.children[1].innerHTML = countNumText;
		btnShowHide(countNumText,_self.parentNode);
		ajaxDishReset(dishid, countNumText, function(){}, function() {});
	});
	function doSelectBtn() {
		var btn = _qAll("article dl .btn");
		var btnIndex = 0,btnLength = btn.length;
		countDish();
		for(btnIndex;btnIndex<btnLength;btnIndex++){
			var countNumText=parseInt(btn[btnIndex].children[1].innerHTML),
					btnAdd=btn[btnIndex].children[2],
					btnMin=btn[btnIndex].children[0];

			btnShowHide(countNumText,btn[btnIndex]);
		} // for

	} // doSelectBtn
	$(".minus").live("click",function(){
		var originalNum,countNumText;
		var _self = this;
		var dishid = _self.parentNode.parentNode.getAttribute('dishid');
		originalNum = parseInt(_self.parentNode.children[1].innerHTML, 10);
		countNumText =  originalNum -1;
		if(originalNum>0){
			var shopInfo =_self.parentNode.parentNode.getAttribute('shopInfo');
			_self.parentNode.children[1].innerHTML = countNumText;
			btnShowHide(countNumText,_self.parentNode);
			ajaxDishReset(dishid, countNumText, function(){}, function() {});
		}else if(originalNum<=0){
			countNumText=0;
			_self.parentNode.children[1].innerHTML = countNumText;
			btnShowHide(countNumText,_self.parentNode);
			ajaxDishRemove(dishid, function(){}, function() {});
		}
	});
    //选择菜品按钮样式

        function ajaxDishReset(dishid, count, successCallback, errorCallback) {
			var urlString = 'call.json?customerId='+ hrefParamsArray["customerId"]+'&userName='+hrefParamsArray["userName"];
			$.ajax({url : urlString,
					dataType : "json",
					type : "post",
					data : {
						'module': 'product',
				        'method' : 'updateOrderDetail',
				        'params' : '{"sid":-1,"pid":' + dishid +  ',"count":' + count + ',"type":"buy"}',
					},
					success : function(data) {
						  if (data['code'] != 0) {
							 	errorCallback();
		                        alert(data['msg']);
		                        return;
		                    } else {
		                    	 successCallback();
		                    }
					}
			});
        } // ajaxDishReset

        function ajaxDishRemove(dishid, successCallback, errorCallback) {
			var urlString = 'call.json?customerId='+ hrefParamsArray["customerId"]+'&userName='+hrefParamsArray["userName"];
			$.ajax({url : urlString,
					dataType : "json",
					type : "post",
					data : {
						'module': 'product',
				        'method' : 'removeOrderDetail',
				        'params' : '{"sid":-1,"pid":' + dishid + ',"type":"buy"}',
					},
					success : function(data) {
						  if (data['code'] != 0) {
		                        alert(data['msg']);
		                        errorCallback();
		                        return;
		                    } else {
		                    	successCallback();
		                    }
					}
			});
        } // ajaxDishRemove
   
    function clearShow(){
        var all = _qAll(".showDetail"),ff = 0;
        for(ff;ff<all.length;ff++){
            _removeClass(all[ff],"sh");
            all[ff].parentNode.parentNode.style.zIndex = '0';
        }
    }

	function doMenu() {
		var params = '{"sid":-1,"type":"buy","tplid":4,"mid":' + hrefParamsArray["mid"] + '}';
		location.href = 'call.html?userName=' + hrefParamsArray['userName'] + '&customerId='
		+ hrefParamsArray['customerId'] + '&module=product&method=getCart&params=' + params;
	}
    
    window.onload = function(){
        setHeight();
      	doSelect();
    	if ({$showBtn} == 1) {
    		doSelectBtn();
    	}
    }
    window.onresize = function(){setHeight();}

	</script>
	              
<script type="text/javascript">
function resetAndAppendHerf(search){
　　	var args = search.substring(1); 
　　	var retval = new Array(); 
	args = args.split("&"); 
	for(var i = 0; i < args.length; i ++){ 
		str = args[i]; 
		var arg = str.split("="); 
		if(arg.length <= 1) continue; 
		if(arg[0] == 'userName'){
			args[i] = "userName=";
		}
	} 
	return args.join("&");
}

$(function(){
	var imageUrl = '{$products.productList[0].picUrl}';
	var title = '{$products.productList[0].name}';
	var des = '点击这里查看商品';
	var search = '?' + resetAndAppendHerf(window.location.search);
	var shareUrlString = window.location.protocol + "//"
			+ window.location.host + window.location.pathname + search;	      
	
	window.shareData = {
		"imgUrl": imageUrl,
		"timeLineLink":  shareUrlString ,
		"sendFriendLink":  shareUrlString ,
		"weiboLink":  shareUrlString ,
		"tTitle": title,
		"tContent": des,
		"fTitle": title,
		"fContent": des,
		"wContent": des
	};
	document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
		// 发送给好友
		WeixinJSBridge.on('menu:share:appmessage', function (argv) {
			WeixinJSBridge.invoke('sendAppMessage', {
				"img_url":  window.shareData.imgUrl,
				"img_width": "640",
				"img_height": "640",
				"link": window.shareData.sendFriendLink,
				"desc": window.shareData.fContent,
				"title": window.shareData.fTitle
			}, function (res) {
				_report('send_msg', res.err_msg);
			})
		});
	
		// 分享到朋友圈
		WeixinJSBridge.on('menu:share:timeline', function (argv) {
			WeixinJSBridge.invoke('shareTimeline', {
				"img_url": window.shareData.imgUrl,
				"img_width": "640",
				"img_height": "640",
				"link": window.shareData.timeLineLink,
				"desc": window.shareData.tContent,
				"title": window.shareData.tTitle
			}, function (res) {
				_report('timeline', res.err_msg);
			});
		});
	
		// 分享到微博
		WeixinJSBridge.on('menu:share:weibo', function (argv) {
			WeixinJSBridge.invoke('shareWeibo', {
				"content": window.shareData.wContent,
				"url": window.shareData.weiboLink
			}, function (res) {
				_report('weibo', res.err_msg);
			});
		});
	});
});
</script>
</body>
</html>